package com.wq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.bo.ArticleTagRelBO;
import com.wq.common.PageResult;
import com.wq.common.Result;
import com.wq.dto.ArticleDeleteDTO;
import com.wq.dto.ArticleDetailDTO;
import com.wq.dto.ArticlePageDTO;
import com.wq.dto.ArticlePublishDTO;
import com.wq.entity.Article;
import com.wq.mapper.ArticleMapper;
import com.wq.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wq.vo.ArticleDetailVO;
import com.wq.vo.ArticlePageVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2024-10-19
 */
@Service
@Transactional
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;


    @Override
    public void publish(ArticlePublishDTO articlePublishDTO) {
        Article article = new Article();
        BeanUtil.copyProperties(articlePublishDTO,article);
        this.save(article);
        List<Integer> tags = articlePublishDTO.getTags();
        List<ArticleTagRelBO> articleTagRelBOList=new ArrayList<>();
        for (Integer tag:tags){
            ArticleTagRelBO articleTagRelBO=new ArticleTagRelBO();
            articleTagRelBO.setArticleId(article.getId());
            articleTagRelBO.setTagId(tag);
            articleTagRelBOList.add(articleTagRelBO);
        }
        articleMapper.saveArticleTagRel(articleTagRelBOList);

    }

    @Override
    public IPage<ArticlePageVO> getPage(ArticlePageDTO articlePageDTO) {
        IPage<Article> pageInfo=new Page<>(articlePageDTO.getCurrent(),articlePageDTO.getSize());

        LambdaQueryWrapper<Article> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(articlePageDTO.getSearchTitle()!=null,Article::getTitle,articlePageDTO.getSearchTitle());
        wrapper.between(articlePageDTO.getStartDate()!=null,Article::getCreateTime,articlePageDTO.getStartDate(),articlePageDTO.getEndDate());
        IPage<Article> page = this.page(pageInfo, wrapper);

        IPage<ArticlePageVO> articlePageVOPage=new Page<>();
        //因为我们最后要的是ArticlePageVO，所以需要将page中的数据复制到articlePageVOPage中
        BeanUtil.copyProperties(page,articlePageVOPage);
        //需要把错误拷贝进去的ArticleList给移除出来换上ArticlePageVO
        List<Article> articles = page.getRecords();
        List<ArticlePageVO> articlePageVOS = new ArrayList<>();
        for (Article article:articles){
            ArticlePageVO articlePageVO=new ArticlePageVO();
            BeanUtil.copyProperties(article,articlePageVO);
            //补全不能直接拷贝的值
            articlePageVO.setCover(article.getTitleImage());
            articlePageVOS.add(articlePageVO);
        }
        //用拷贝好的articlePageVOS替换掉page中的数据
        articlePageVOPage.setRecords(articlePageVOS);
        return articlePageVOPage ;
    }

    @Override
    public void removeArticle(ArticleDeleteDTO articleDeleteDTO) {
        this.removeById(articleDeleteDTO.getArticleId());
        //删除文章和标签的关联
        articleMapper.deleteArticelleTagRel(articleDeleteDTO);
    }

    @Override
    public ArticleDetailVO detail(ArticleDetailDTO articleDetailDTO) {
        Article article = this.getById(articleDetailDTO.getArticleId());
        ArticleDetailVO articleDetailVO=new ArticleDetailVO();
        BeanUtil.copyProperties(article,articleDetailVO);
        articleDetailVO.setSummary(article.getDescription());
        articleDetailVO.setCover(article.getTitleImage());
        articleDetailVO.setTagIds(articleMapper.getTagIdsByArticleId(articleDetailDTO.getArticleId()));
        return articleDetailVO;
    }


}
